package com.example.demo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.demo.R;
import com.example.demo.entity.Singer;

import java.util.List;

public class SingerAdapter extends RecyclerView.Adapter<SingerAdapter.SingerViewHolder> {
    private List<Singer> singerList;
    private Context context;

    public SingerAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<Singer> singerList){
        this.singerList = singerList;
    }

    @NonNull
    @Override
    public SingerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SingerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_singer, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull SingerViewHolder holder, int position) {
        Singer singer = singerList.get(position);
        Glide.with(context)
                .load(singer.getPicUrl())
                .into(holder.img);
        holder.tvName.setText(singer.getName());
    }

    @Override
    public int getItemCount() {
        return singerList == null ? 0 : singerList.size();
    }

    public class SingerViewHolder extends RecyclerView.ViewHolder {
        ImageView img;
        TextView tvName;
        public SingerViewHolder(@NonNull View itemView) {
            super(itemView);

            img = itemView.findViewById(R.id.img_singer);
            tvName = itemView.findViewById(R.id.tv_name);
        }
    }
}
